﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Core;
using Moq;

namespace ZDSoft.LMS.UnitTest
{
    public class TestBase
    {
        public TestBase()
        {
            if (!ActiveRecordStarter.IsInitialized)//如果ActiveRecordStarter框架没有初始化
            {
                IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;//初始化配置文件
                ActiveRecordStarter.Initialize(typeof(ZDSoft.LMS.Domain.User).Assembly, source);//初始化程序信
            }

        }

        /// <summary>
        /// 使用Mock框架模拟对象
        /// </summary>
        /// <returns></returns>
        public HttpContextBase CreateHttpContext()
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            response
                .Setup(rsp => rsp.ApplyAppPathModifier(Moq.It.IsAny<string>()))
                .Returns((string s) => s);

            context.Setup(ctx => ctx.Request).Returns(request.Object);
            context.Setup(ctx => ctx.Response).Returns(response.Object);
            context.Setup(ctx => ctx.Session).Returns(session.Object);
            context.Setup(ctx => ctx.Server).Returns(server.Object);

            return context.Object;
        }

    }
}
